
package combat.dudes.server.protocol.implementation;

import gnu.cajo.invoke.Remote;

import java.rmi.RemoteException;

import combat.dudes.DudesFunctions;
import combat.dudes.protocol.interfaces.UserInt;
import combat.dudes.protocol.interfaces.VehicleInt;
import combat.dudes.server.ServerApp;


/**
 * The user class. Note that once a vehicle is set to a user, that vehicle's
 * 'locked' status is set as true (see setUserVehicle()).
 * @author Laurens Andriessen
 */
public class User extends NewID.UserID implements UserInt {

	private static final long serialVersionUID = 1L;

	private int userID = -1;
	private String userName = "Anonymous (" + userID + ")";
	private VehicleInt userVehicle = null;

	public User() {
		this.userID = getID();
	}

	public User(String userName) {
		this.userID = getID();
		this.userName = userName;
	}

	public User(int userID, String userName) {
		// this.userID = userID;
		this.userID = getID();
		this.userName = userName;
	}

	@Override
	public int getUserID() {
		return userID;
	}
	@Override
	public void setUserID(int userID) {
		this.userID = userID;
	}
	@Override
	public String getUserName() {
		return userName;
	}
	@Override
	public void setUserName(String userName) {
		this.userName = userName;
	}

	@Override
	public VehicleInt getUserVehicle() {
		return userVehicle;
	}
	@Override
	public void setUserVehicle(VehicleInt userVehicle) {
		if(userVehicle != null)
			if(DudesFunctions.Network.isProxy(userVehicle))
				userVehicle = (VehicleInt) Simulation.instance().findPhysicalObject(userVehicle);
		if(userVehicle != null)
			userVehicle.lock();
		this.userVehicle = userVehicle;
	}

	@Override
	public Remote getUserVehicle_Remote() {
		try {
			return new Remote(getUserVehicle());
		} catch(RemoteException e) {
			ServerApp.onError("@User.java, getUserVehicle_Remote()", e, false);
		}
		return null;
	}


}
